package cn.qidd.keep.member.resource;

import cn.qidd.keep.domain.core.dict.MemberStatus;
import cn.qidd.keep.domain.document.Member;
import cn.qidd.keep.domain.document.QMember;
import cn.qidd.keep.feignclient.member.IMemberService;
import cn.qidd.keep.member.repository.MemberRepository;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
public class MemberResource implements IMemberService {

    @Resource
    private MemberRepository memberRepository;

    @Override
    public boolean isExist(@PathVariable String memberNo) {
        return memberRepository.exists(QMember.member.memberNo.eq(memberNo));
    }

    @Override
    public Member getMemberByMemberNo(@PathVariable String memberNo) {
        return memberRepository.findOne(QMember.member.memberNo.eq(memberNo)).orElse(null);
    }

    @Override
    public void updateMember(@PathVariable String memberNo) {
        Member member = memberRepository.findOne(QMember.member.memberNo.eq(memberNo)).orElseThrow(RuntimeException::new);
        Integer leaveCount = member.getLeaveCount();
        member.setLeaveCount(leaveCount - 1);
        if (leaveCount == 1) {
            member.setMemberStatus(MemberStatus.FINISHED);
        }
        member.setLastTime(new Date());
        memberRepository.save(member);
    }
}
